$(function () {
    var gspzId = $("#gspzForm [name='gspz.id']").val();

    if (gspzId) {
        var url = $.webPath() + "/base/gspz/adsqList?gspzId=" + gspzId;
        gspz_adsq_datagrid(url);
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'fkfs'}, function (data) {
            var url = $.webPath() + "/base/gspz/atsqList?gspzId=" + gspzId;
            gspz_atsq_datagrid(data, url)
        })
    } else {
        gspz_adsq_datagrid(null)
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'fkfs'}, function (data) {
            gspz_atsq_datagrid(data, -1)
        })
    }


    $("#gspz_adsq_tj").click(function () {
        $("#gspz_adsq_table").edatagrid("saveRow");
        var data = $("#gspz_adsq_table").datagrid("getRows");
        var row = data[data.length - 1];
        $('#gspz_adsq_table').datagrid('appendRow', {
            startDate: null,
            endDate: null,
            sfbz: ''
        });
    });
    $("#gspz_adsq_sc").click(function () {
        var row = $("#gspz_adsq_table").edatagrid("getSelected");
        if (row) {
            var index = $("#gspz_adsq_table").edatagrid("getRowIndex", row);
            $("#gspz_adsq_table").edatagrid("deleteRow", index);
        }
    });


    $("#gspz_atsq_tj").click(function () {
        $("#gspz_atsq_table").edatagrid("saveRow");
        var data = $("#gspz_atsq_table").datagrid("getRows");
        var row = data[data.length - 1];
        if (row && row.endDate) {
            gspz_newDataRow(parseFloat(row.endDate) + 1, "");
        } else {
            gspz_newDataRow(0, "");
        }
    });
    $("#gspz_atsq_sc").click(function () {
        var row = $("#gspz_atsq_table").edatagrid("getSelected");
        if (row) {
            var index = $("#gspz_atsq_table").edatagrid("getRowIndex", row);
            $("#gspz_atsq_table").edatagrid("deleteRow", index);
        }
    });
});


function gspz_newDataRow(fksjStart, fksj) {
    $('#gspz_atsq_table').datagrid('appendRow', {
        fklx_z: '',
        startDate: fksjStart,
        endDate: fksj,
        llgs: ''
    });
}


function gspz_adsq_datagrid(url) {

    $("#gspz_adsq_table").edatagrid({
        height:268,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        url: url,
        columns: [
            [
                {field: 'startDate', title: '开始时间', width: 100, editor: {type: 'datebox'}},
                {field: 'endDate', title: '结束时间', width: 100, editor: {type: 'datebox'}},
                {field: 'sfbz', title: '收费标准(元/吨)', width: 100, editor: {type: 'numberbox', options: {min: 0, precision: 4, groupSeparator: ','}}}
            ]
        ]
    });

}

function gspz_atsq_datagrid(fsOptions, url) {

    $("#gspz_atsq_table").edatagrid({
        height:268,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        url: url,
        columns: [
            [
                {field: 'fklx.id', editor: {type: 'combobox', options: {
                    data: fsOptions,
                    valueField: 'id',
                    textField: 'z',
                    multiple: false,
                    panelHeight: '55'
                }
                }, formatter: function (value) {
                    for (var i = 0; i < fsOptions.length; i++) {
                        if (fsOptions[i].id == value) return fsOptions[i].z;
                    }
                    return value;
                }, title: '付款类型', width: 50},
                {field: 'startDate', title: '周期开始时间(天)', width: 100, editor: {type: 'numberbox', options: { min: 0}}},
                {field: 'endDate', title: '周期结束时间(天)', width: 100,  editor: {type: 'numberbox', options: { min: 0}}},
                {field: 'llgs', title: '月收费标准(%)', width: 100,  editor: {type: 'text'}}
            ]
        ]
    });

}


